let request = require('../utils/request')
import Toast from '../miniprogram_npm/@vant/weapp/toast/toast'
// 基础设置的基本url
let baseUrl = '/User_Base_Servlet/'
// 获取药崽风格配置
function getUserStyle(){
  return new Promise((resolve,reject) => {
    request({
      url:baseUrl+'get_user_style'
    }).then(res=> {
      const { State, UserAppList } = res;
      // 通过返回的State判断数据是否获取成功
      State ? resolve(UserAppList.style) : reject('请求数据失败')
    })
  })
}

// 更新药崽风格配置
function updateUserStyle(data){
  return new Promise((resolve,reject) => {
    request({
      url:baseUrl+'update_user_style',
      data
    })
      .then(res=>{
        resolve(res)
      })    
      .catch(err=>{
        reject(err)
      })
  })
}

// 获取病例
function getDiseaseHistory(){
  return new Promise((resolve,reject) => {
    request({
      url:'/Person_Servlet/get_disease_history'
    })
      .then(res=>{
        resolve(res)
      })
      .catch(err=>{
        reject(err)
      })
  })
}
// 获取基本信息
function getPersonalBase(){
  return new Promise((resolve,reject) => {
    request({
      url:'/Person_Servlet/get_person_base_info'
    })
      .then(res=>{
        resolve(res)
      })
      .catch(err=>{
        reject(err)
      })
  })
}

// 获取日记列表
function getDiaryList(){
  return new Promise((resolve,reject) => {
    request({
      url:'/Person_Servlet/get_diary_list'
    })
      .then(res=>{
        resolve(res)
      })
      .catch(err=>{
        reject(err)
      })
  })
}

// 设置手机号码
function setPhoneNumber(data){
  return new Promise((resolve,reject) => {
    request({
      url:'/User_Base_Servlet/set_user_telephone?',
      data
    })
      .then(res=>{
        let { State } = res
        if(State){
          Toast.success('保存成功');
          wx.setStorage({
            key:"phone",
            data:data.telephone
          })
        }
      })
      .catch(err=>{
        reject(err)
      })
  })
}
/**
 * 家庭成员模块
 * @function getFamilyList 获取家庭成员列表
 * @function sendApplication 发送组建申请
 * @function getApplicationList 获取申请列表
 * @function changeApplication 改变申请状态 
 * @function getFamilyInfo 获取家庭成员具体信息
 */
// 基础api模块设置
let familyBaseUrl = '/FamilyServlet/'

function familyApi(url,data='',openid=true){
  this.url = familyBaseUrl+url,
  this.data = data,
  this.openid = openid
}
familyApi.prototype.request = function(){
  return new Promise((resolve,reject) => {
    request({
      url:this.url,
      data:this.data
    },this.openid)
      .then(res=>{
        resolve(res)
      })
      .catch(err=>{
        reject(err)
      })
  })
}

// 获取家庭成员列表
let getFamilyList = new familyApi('get_family_list')
// 验证发出的申请号码是否存在
function inquiryTelephone(data){
  return new Promise((resolve,reject) => {
    request({
      url:'/User_Base_Servlet/inquiry_user_by_telephone?',
      data
    })
      .then(res=>{
        let { State } = res
        if(!State){
          Toast.fail('用户不存在');
        }
      })
      .catch(err=>{
        reject(err)
      })
  })
}
// 发起组建家庭申请
function sendApplication(data){
  let send = new familyApi('send_application',data,false)
    send.request().then(res=>{
      const { State } = res
      if(State){
        Toast.success('发送成功');
      }else{
        inquiryTelephone({telephone:data.telephone})
      }
    })
}
// 获取申请列表
let getApplicationList = new familyApi('get_application')
// 申请请求处理
function changeApplication(data){
  return new familyApi('change_application_state',data,false).request()
}
// 获取家庭成员相关提醒信息
function getFamilyInfo(data){
  return new familyApi('get_remind_list_simple',data,false).request()
}

module.exports = {
  updateUserStyle,
  getUserStyle,
  getDiseaseHistory,
  getPersonalBase,
  getDiaryList,
  setPhoneNumber,
  getFamilyList,
  sendApplication,
  getApplicationList,
  changeApplication,
  getFamilyInfo
}